from flask import Blueprint, jsonify, request

from application.services.system.menu import MenuService
from application.utils.res import json_fail, json_success

bp = Blueprint("menu", __name__, url_prefix="/menu")


@bp.get("/getRouters")
def get_Routers():
    """
    获取用户信息接口
    返回用户的基本信息、角色和按钮权限
    """
    response_data = {
        "code": 200,
        "msg": "操作成功",
        "data": [
            {
                "name": "System1",
                "path": "/system",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "系统管理",
                    "icon": "carbon:cloud-service-management",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "User100",
                        "path": "user",
                        "hidden": False,
                        "component": "system/user/index",
                        "meta": {
                            "title": "用户管理",
                            "icon": "ic:round-manage-accounts",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Role101",
                        "path": "role",
                        "hidden": False,
                        "component": "system/role/index",
                        "meta": {
                            "title": "角色管理",
                            "icon": "carbon:user-role",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Menu102",
                        "path": "menu",
                        "hidden": False,
                        "component": "system/menu/index",
                        "meta": {
                            "title": "菜单管理",
                            "icon": "material-symbols:route",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Dept103",
                        "path": "dept",
                        "hidden": False,
                        "component": "system/dept/index",
                        "meta": {
                            "title": "部门管理",
                            "icon": "mingcute:department-line",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Post104",
                        "path": "post",
                        "hidden": False,
                        "component": "system/post/index",
                        "meta": {
                            "title": "岗位管理",
                            "icon": "hugeicons:permanent-job",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Dict105",
                        "path": "dict",
                        "hidden": False,
                        "component": "system/dict/index",
                        "meta": {
                            "title": "字典管理",
                            "icon": "qlementine-icons:dictionary-16",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Config106",
                        "path": "config",
                        "hidden": False,
                        "component": "system/config/index",
                        "meta": {
                            "title": "参数设置",
                            "icon": "carbon:parameter",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Notice107",
                        "path": "notice",
                        "hidden": False,
                        "component": "system/notice/index",
                        "meta": {
                            "title": "通知公告",
                            "icon": "solar:chat-line-outline",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Log108",
                        "path": "log",
                        "hidden": False,
                        "redirect": "noRedirect",
                        "component": "Layout",
                        "alwaysShow": True,
                        "meta": {
                            "title": "日志管理",
                            "icon": "tabler:logs",
                            "noCache": False,
                            "link": None,
                        },
                        "children": [
                            {
                                "name": "Operlog500",
                                "path": "operlog",
                                "hidden": False,
                                "component": "monitor/operlog/index",
                                "meta": {
                                    "title": "操作日志",
                                    "icon": "carbon:operations-record",
                                    "noCache": False,
                                    "link": None,
                                },
                            },
                            {
                                "name": "Logininfor501",
                                "path": "logininfor",
                                "hidden": False,
                                "component": "monitor/logininfor/index",
                                "meta": {
                                    "title": "登录日志",
                                    "icon": "tabler:login-2",
                                    "noCache": False,
                                    "link": None,
                                },
                            },
                        ],
                    },
                    {
                        "name": "Oss118",
                        "path": "oss",
                        "hidden": False,
                        "component": "system/oss/index",
                        "meta": {
                            "title": "文件管理",
                            "icon": "material-symbols:attach-file",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Oss/config133",
                        "path": "oss/config",
                        "hidden": True,
                        "component": "system/oss-config/index",
                        "meta": {
                            "title": "文件配置管理",
                            "icon": "hugeicons:configuration-01",
                            "noCache": True,
                            "link": None,
                        },
                    },
                    {
                        "name": "Client123",
                        "path": "client",
                        "hidden": False,
                        "component": "system/client/index",
                        "meta": {
                            "title": "客户端管理",
                            "icon": "tabler:device-imac-cog",
                            "noCache": False,
                            "link": None,
                        },
                    },
                ],
            },
            {
                "name": "Tenant6",
                "path": "/tenant",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "租户管理",
                    "icon": "tabler:building-cog",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "Tenant121",
                        "path": "tenant",
                        "hidden": False,
                        "component": "system/tenant/index",
                        "meta": {
                            "title": "租户管理",
                            "icon": "tabler:building-skyscraper",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "TenantPackage122",
                        "path": "tenantPackage",
                        "hidden": False,
                        "component": "system/tenantPackage/index",
                        "meta": {
                            "title": "租户套餐管理",
                            "icon": "lets-icons:package-box-alt",
                            "noCache": False,
                            "link": None,
                        },
                    },
                ],
            },
            {
                "name": "Monitor2",
                "path": "/monitor",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "系统监控",
                    "icon": "stash:dashboard",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "Online109",
                        "path": "online",
                        "hidden": False,
                        "component": "monitor/online/index",
                        "meta": {
                            "title": "在线用户",
                            "icon": "majesticons:status-online-line",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Cache113",
                        "path": "cache",
                        "hidden": False,
                        "component": "monitor/cache/index",
                        "meta": {
                            "title": "缓存监控",
                            "icon": "simple-icons:redis",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Admin117",
                        "path": "Admin",
                        "hidden": False,
                        "component": "FrameView",
                        "query": "https://ruoyi.xlsea.cn/admin/",
                        "meta": {
                            "title": "Admin监控",
                            "icon": "bx:bxl-spring-boot",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Snailjob120",
                        "path": "snailjob",
                        "hidden": False,
                        "component": "FrameView",
                        "query": "https://preview.snailjob.opensnail.com/",
                        "meta": {
                            "title": "任务调度中心",
                            "icon": "gridicons:scheduled",
                            "noCache": False,
                            "link": None,
                        },
                    },
                ],
            },
            {
                "name": "Tool3",
                "path": "/tool",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "系统工具",
                    "icon": "tabler:tools",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "Gen115",
                        "path": "gen",
                        "hidden": False,
                        "component": "tool/gen/index",
                        "meta": {
                            "title": "代码生成",
                            "icon": "material-symbols:code-blocks-outline",
                            "noCache": False,
                            "link": None,
                        },
                    }
                ],
            },
            {
                "name": "Demo5",
                "path": "/demo",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "测试菜单",
                    "icon": "material-symbols:kid-star-outline",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "Demo1500",
                        "path": "demo",
                        "hidden": False,
                        "component": "demo/demo/index",
                        "meta": {
                            "title": "测试单表",
                            "icon": "gg:debug",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Tree1506",
                        "path": "tree",
                        "hidden": False,
                        "component": "demo/tree/index",
                        "meta": {
                            "title": "测试树表",
                            "icon": "gg:debug",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "Leave11638",
                        "path": "leave",
                        "hidden": False,
                        "component": "workflow/leave/index",
                        "meta": {
                            "title": "请假申请",
                            "icon": "ic:twotone-time-to-leave",
                            "noCache": False,
                            "link": None,
                        },
                    },
                ],
            },
            {
                "name": "Workflow11616",
                "path": "/workflow",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "工作流",
                    "icon": "hugeicons:flow-square",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "Category11622",
                        "path": "category",
                        "hidden": False,
                        "component": "workflow/category/index",
                        "meta": {
                            "title": "流程分类",
                            "icon": "carbon:category",
                            "noCache": False,
                            "link": None,
                        },
                    },
                    {
                        "name": "ProcessDefinition11620",
                        "path": "processDefinition",
                        "hidden": False,
                        "component": "workflow/processDefinition/index",
                        "meta": {
                            "title": "流程定义",
                            "icon": "weui:setting-outlined",
                            "noCache": True,
                            "link": None,
                        },
                    },
                    {
                        "name": "Monitor11630",
                        "path": "monitor",
                        "hidden": False,
                        "redirect": "noRedirect",
                        "component": "Layout",
                        "alwaysShow": True,
                        "meta": {
                            "title": "流程监控",
                            "icon": "lucide:monitor-cog",
                            "noCache": False,
                            "link": None,
                        },
                        "children": [
                            {
                                "name": "ProcessInstance11621",
                                "path": "processInstance",
                                "hidden": False,
                                "component": "workflow/processInstance/index",
                                "meta": {
                                    "title": "流程实例",
                                    "icon": "ri:instance-line",
                                    "noCache": True,
                                    "link": None,
                                },
                            },
                            {
                                "name": "AllTaskWaiting11631",
                                "path": "allTaskWaiting",
                                "hidden": False,
                                "component": "workflow/task/allTaskWaiting/index",
                                "meta": {
                                    "title": "待办任务",
                                    "icon": "ri:todo-line",
                                    "noCache": True,
                                    "link": None,
                                },
                            },
                        ],
                    },
                    {
                        "name": "Design11700",
                        "path": "design",
                        "hidden": True,
                        "component": "workflow/design/index",
                        "meta": {
                            "title": "流程设计",
                            "icon": "material-symbols:design-services-outline",
                            "noCache": True,
                            "link": None,
                        },
                    },
                ],
            },
            {
                "name": "Task11618",
                "path": "/task",
                "hidden": False,
                "redirect": "noRedirect",
                "component": "Layout",
                "alwaysShow": True,
                "meta": {
                    "title": "我的任务",
                    "icon": "fluent:notepad-person-16-regular",
                    "noCache": False,
                    "link": None,
                },
                "children": [
                    {
                        "name": "MyDocument11629",
                        "path": "myDocument",
                        "hidden": False,
                        "component": "workflow/task/myDocument/index",
                        "meta": {
                            "title": "我发起的",
                            "icon": "hugeicons:start-up-02",
                            "noCache": True,
                            "link": None,
                        },
                    },
                    {
                        "name": "TaskWaiting11619",
                        "path": "taskWaiting",
                        "hidden": False,
                        "component": "workflow/task/taskWaiting/index",
                        "meta": {
                            "title": "我的待办",
                            "icon": "ri:todo-line",
                            "noCache": True,
                            "link": None,
                        },
                    },
                    {
                        "name": "TaskFinish11632",
                        "path": "taskFinish",
                        "hidden": False,
                        "component": "workflow/task/taskFinish/index",
                        "meta": {
                            "title": "我的已办",
                            "icon": "hugeicons:task-done-01",
                            "noCache": True,
                            "link": None,
                        },
                    },
                    {
                        "name": "TaskCopy11633",
                        "path": "taskCopy",
                        "hidden": False,
                        "component": "workflow/task/taskCopy/index",
                        "meta": {
                            "title": "我的抄送",
                            "icon": "mynaui:copy",
                            "noCache": True,
                            "link": None,
                        },
                    },
                ],
            },
            {
                "name": "Https://gitee.com/dromara/RuoYi-Vue-Plus4",
                "path": "https://gitee.com/dromara/RuoYi-Vue-Plus",
                "hidden": False,
                "component": "FrameView",
                "meta": {
                    "title": "RuoYi-Vue-Plus",
                    "icon": "local-icon-gitee",
                    "noCache": False,
                    "link": "https://gitee.com/dromara/RuoYi-Vue-Plus",
                },
            },
            {
                "name": "Https://github.com/soybeanjs7",
                "path": "https://github.com/soybeanjs",
                "hidden": False,
                "component": "FrameView",
                "meta": {
                    "title": "Soybean Admin",
                    "icon": "mdi:github",
                    "noCache": False,
                    "link": "https://github.com/soybeanjs",
                },
            },
            {
                "name": "Https://gitee.com/xlsea/ruoyi-plus-soybean8",
                "path": "https://gitee.com/xlsea/ruoyi-plus-soybean",
                "hidden": False,
                "component": "FrameView",
                "meta": {
                    "title": "RuoYi-Plus-Soybean",
                    "icon": "local-icon-gitee",
                    "noCache": False,
                    "link": "https://gitee.com/xlsea/ruoyi-plus-soybean",
                },
            },
        ],
    }

    return jsonify(response_data)


@bp.get("/list")
def get_Menu_List():
    """
    获取菜单列表数据

    Args:
        query_params: 查询参数字典
            - parent_id: 父菜单ID
            - menu_type: 菜单类型

    Returns:
        List[Dict[str, Any]]: 菜单列表数据
    """
    try:
        # 接收查询参数
        parent_id = request.args.get("parentId", "").strip()
        menu_type = request.args.get("menuType", "").strip()

        # 构造查询参数字典
        query_params = {}
        if parent_id:
            query_params["parent_id"] = parent_id
        if menu_type:
            query_params["menu_type"] = menu_type

        # 调用 service 层获取菜单列表数据
        menu_list = MenuService.get_menu_list(query_params)
        return json_success(menu_list)

    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500
